﻿using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "2021-07-10";
            DateTime billdate = Convert.ToDateTime("2021-07-06");
            DateTime time = Convert.ToDateTime(str);
            DateTime edate = time.AddMonths(-1).AddDays(1 - time.Day).AddDays(9);

            var diffdays = (billdate - edate).Days;
            Console.WriteLine(edate);
            Console.WriteLine(diffdays);
            #region TEST是否自然月
            DateTime time1 = Convert.ToDateTime("2021-07-01");
            DateTime time2 = Convert.ToDateTime("2021-08-31");
            var isnaturalMonth = isNaturalMonth(time1, time2);
            Console.WriteLine(isnaturalMonth);
            #endregion

            {
                A a = new A();
                //a.b = 8m;
                //a.b = a.b ?? 0;
                a.b = a.b.GetValueOrDefault() + 9.00m;

            }
            Console.ReadLine();
        }


        /// <summary>
        ///  判断两个时间间隔是否是自然月
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public static bool isNaturalMonth(DateTime date1, DateTime date2)
        {
            int startYear = date1.Year;
            int startMonth = date1.Month;
            int startDay = date1.Day;
            int endYear = date2.Year;
            int endMonth = date2.Month;
            int endDay = date2.Day;
            if (startYear != endYear)
            {
                return false;
            }
            if (startMonth != endMonth)
            {
                return false;
            }

            if (startDay == 1)
            {
                if (endMonth == 2)
                {
                    if (endYear % 4 == 0 && endYear % 100 != 0 || endYear % 400 == 0)
                    {
                        if (endDay == 29)
                            return true;
                    }
                    else
                    {
                        if (endDay == 28)
                            return true;
                    }
                }
                else
                {
                    if (endMonth == 1 || endMonth == 3 || endMonth == 5 || endMonth == 7 || endMonth == 8 || endMonth == 10 || endMonth == 12)
                    {
                        if (endDay == 31)
                            return true;
                    }
                    else
                    {
                        if (endDay == 30)
                            return true;
                    }
                }
            }
            return false;


        }

        public class A
        {
            public int a { get; set; }

            public Nullable<decimal> b { get; set; }

        }
       
    }

}
